<template>
	<view>
		<baseForm :column.sync="column" @submitFun="submitFun" @changeColumn="changeColumn" :submitStatus="submitDisable"></baseForm>
	</view>
</template>

<script>
import baseForm from '@/wxcomponents/form/form.vue';
import formMixins from '@/libs/formMixins.js';
export default {
	mixins: [formMixins],
	data() {
		return {
			// 选中印章对象
			chooseSeal: '',
			// 当前印章列表
			sealList: [],
			column: [
				{
					label: '印章名称',
					name: 'sealId',
					type: 'picker',
					mode: 'selector',
					required: true,
					disabled: false,
					value: '',
					text: '请选择',
					range: []
				},
				{
					label: '所属部门',
					name: 'sealDepartmentName',
					type: 'normalInput',
					disabled: true,
					required: false,
					value: '',
					text: '',
				},
				{
					label: '用章原因',
					name: 'applyReason',
					type: 'normalInput',
					disabled: false,
					required: false,
					value: ''
				},
				{
					label: '附件(最多上传三张)',
					type: 'upload',
					url: '/stu/common/uploadFile',
					docFileId: '',
					docFileUrl: '',
					attachmentVoListStr: '',
					limitSize: 3,
					disabled: false
				}
			],
		};
	},
	async onLoad() {
		await this.fetchSealList();
		// 监听印章变化
		this.EventBus.on('columnItemChange',(obj) => {
			if (obj.key == 'sealId') {
				this.updateChooseSeal(obj);
			}
		})
	},
	methods: {
		/**
		 * 获取印章列表数据
		 * */
		async fetchSealList() {
			let resp = await this.$http.get({
				url: '/oa/sealRegister/list'
			});
			let columnItem = this.getColumnItemByKeyAndValue(this.column, 'name', 'sealId')
			this.sealList = columnItem.range = this.getChoicesByResp(resp, {nameKey: 'sealName', valueKey: 'id'});
		},
		/**
		 * 更新选中印章,并更新印章部门表单字段
		 * */
		updateChooseSeal(obj) {
			// 选中
			this.chooseSeal = this.sealList.filter(item => {
				return item.value == obj.value;
			})[0];
			// 更新部门字段
			let columnItem = this.getColumnItemByKeyAndValue(this.column, 'name', 'sealDepartmentName')
			columnItem.value = columnItem.text = this.chooseSeal.departmentName;
		},
		async submitFun() {
			let seal = this.chooseSeal;
			let reasonCol =  this.getColumnItemByKeyAndValue(this.column, 'name', 'applyReason')
			let params = {
				sealId: seal.id,
				sealName: seal.sealName,
				sealDepartmentId: seal.departmentId,
				sealDepartmentName: seal.departmentName,
				applyReason: reasonCol.value,
				attachmentVoListStr: this.column[3].attachmentVoListStr, //附件列表
			};
			let rules = [
				{
					key: 'sealId',
					checkFunc(value) {
						if (!value)	return false;
						return true;
					},
					errMsg: '请选择印章'
				},
			]
			let checkRes = this.checkFormIsPassRules(params, rules);
			if (!checkRes.isPass) {
				this.showToast(checkRes.errMsg);
				return;
			}
			let resp = await this.$http.post({
				url: '/oa/sealApply/save',
				data: params
			});
			this.showToast('提交成功',()=> {uni.navigateBack()});
		},
	},
	components: {
		baseForm
	},
};
</script>

<style></style>
